<template>
  <div>
    <el-dialog
      :title="addData.id ? '编辑分类' : '新建分类'"
      v-model="isDialog"
      width="500px"
      :before-close="closeClassAdd"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="classForm"
        label-width="110px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input
            type="text"
            placeholder="请输入分类名称"
            v-model.trim="formData.name"
            maxlength="20"
            :show-word-limit="true"
          >
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="选择组件icon" prop="remark">
          <el-tag
            v-for="(item, idx) in iconList"
            :key="idx"
            :class="{ 'tag-active': formData.icon == item }"
            @click="formData.icon = item"
          >
            <i :class="item"></i>
          </el-tag>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="请输入备注内容"
            v-model.trim="formData.remark"
            maxlength="50"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeClassAdd()">取 消</el-button>
          <el-button type="primary" @click="formValidate">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import EventApi from "@/api/eventApi";
export default {
  props: {
    isAdd: {
      require: false,
      type: Boolean,
      default: () => {
        return false;
      },
    },
    addData: {
      require: false,
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    isAdd(val) {
      if (val && this.addData.id) {
        this.formData = this.addData;
      }
      this.isDialog = val;
    },
  },
  data() {
    return {
      isDialog: false,
      formData: {
        name: "",
        icon: "",
        remark: "",
      },
      typeList: [
        { id: 0, name: "数字" },
        { id: 1, name: "字符串" },
        { id: 2, name: "时间" },
        { id: 3, name: "日期" },
      ],
      rules: {
        name: [{ required: true, message: "请输入属性名称", trigger: "blur" }],
      },
      iconList: [
        "el-icon-tickets",
        "el-icon-document-checked",
        "el-icon-attract",
        "el-icon-data-analysis",
      ],
    };
  },
  methods: {
    formValidate() {
      this.$refs.classForm.validate((valid) => {
        if (valid) {
          this.saveEventClassify();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    async saveEventClassify() {
      let rs = await EventApi.saveEventClassify(this.formData);
      if (rs.code == 200) {
        ElMessage.success(
          this.addData.id ? "修改分类成功" : "添加分类成功"
        );
        this.$emit("addAttribute");
        this.$refs.classForm.resetFields();
      }
    },
    closeClassAdd() {
      this.$emit("closeClassAdd");
      this.$refs.classForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tag {
  margin-right: 10px;
  cursor: pointer;
}
.tag-active {
  color: red;
}
</style>